<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      table,
      td {
        border: 1px solid pink;
        border-collapse: collapse;
      }
      td {
        width: 80px;
        height: 20x;
      }
    </style>
  </head>
  <body>
    <script>
      var arr = [
        { name: "jake", age: 18, gender: "男" },
        { name: "rose", age: 18, gender: "女" },
        { name: "mary", age: 24, gender: "女" },
        { name: "mary", age: 24, gender: "女" },
      ];
      var tableEle = document.createElement("table");
      document.body.appendChild(tableEle); //在页面中构建表单
      var tableHead = document.createElement("thead");
      tableHead.innerHTML =
        "<td>姓名</td><td>年龄</td><td>性别</td><td>操作</td>";
      tableEle.appendChild(tableHead); //构建表头

      arr.forEach(function (item, i, arr) {
        var tables = document.createElement("tr");
        if (i % 2 == 0) {
          tables.style.background = "#e18a8a";
          //   tables.setAttribute("bgcolor", "#e18a8a"); //用添加属性写
        }
        tables.innerHTML = `<td>${item.name}</td><td>${item.age}</td><td>${item.gender}</td><td><button>删除</button></td>`;
        tableEle.appendChild(tables);
      });
      //循环td构建的。。
      // var tableEle = document.createElement("table");
      // document.body.appendChild(tableEle); //在页面中构建表单
      // var tableHead = document.createElement("thead");
      // tableHead.innerHTML =
      //   "<td>姓名</td><td>年龄</td><td>性别</td><td>操作</td>";
      // tableEle.appendChild(tableHead); //构建表头

      // arr.forEach(function (item, i, arr) {
      //   var tables = document.createElement("tr");
      //   if (i % 2 == 0) {
      //     tables.style.background = "#e18a8a";
      //     //   tables.setAttribute("bgcolor", "#e18a8a"); //用添加属性写
      //   }

      //   for (var k in arr[i]) {
      //     var tdEle = document.createElement("td");
      //     tdEle.innerHTML = arr[i][k];
      //     tables.appendChild(tdEle);
      //   }
      //   var tdEle = document.createElement("td");
      //   tdEle.innerHTML = "<td><button>删除</button></td>";
      //   tables.appendChild(tdEle);
      //   tableEle.appendChild(tables);
      // });
    </script>
  </body>
</html>
